/**
 *	@author btan
 *	@email: btan@tma.com.vn - anbtan@gmail.com
 */
package com.vital.mshopping;

import java.util.List;

import com.vital.dao.Category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MainAdapter extends ArrayAdapter<Category>{
	//---Member variable---
	Context mContext;
	int resourceId;
	List<Category> categories;

	public MainAdapter(Context context, int textViewResourceId,
			List<Category> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		
		mContext = context;
		resourceId = textViewResourceId;
		categories = objects;
	}

	/**
	 * getView
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		if (v == null) {
			LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = li.inflate(resourceId, null);
		}
		
		Category category = categories.get(position);
		if (category != null) {
			// Fill data
			ImageView imgView = (ImageView)v.findViewById(R.id.imgView);
			TextView tvCategory = (TextView)v.findViewById(R.id.tvCategory);
			
			imgView.setBackgroundResource(mContext.getResources().getIdentifier(category.getCateIcon(),
													"drawable", mContext.getPackageName()));
			tvCategory.setText(category.getCateName());
		}
		
		return v;
	}
}
